package org.example.loadbalancer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

/**
 * Application
 *
 * @author zhengchalei
 * @date 2021/09/26
 */
@EnableFeignClients
@SpringBootApplication
public class OpenFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(OpenFeignApplication.class, args);
    }

    @FeignClient("ECHO")
    interface EchoService {
        /**
         * 回声
         *
         * @param msg msg
         * @return {@link Mono}<{@link String}>
         */
        @RequestMapping("/echo")
        String echo(@RequestParam("msg") String msg);
    }


    @RestController
    static class Echo {

        @Autowired
        private EchoService echoService;

        @RequestMapping("/")
        public String echo(@RequestParam(defaultValue = "loadBalancer-service", required = false) String msg) {
            return this.echoService.echo(msg);
        }
    }
}
